package edu.iyte.ceng431.hw2.pos.view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BarChartView extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	List<Double> cashTotals;
	List<Double> cardTotals;
	String dt1;
	String dt2;

	public String getDt1() {
		return dt1;
	}

	public void setDt1(String dt1) {
		this.dt1 = dt1;
	}

	public String getDt2() {
		return dt2;
	}

	public void setDt2(String dt2) {
		this.dt2 = dt2;
	}

	public List<Double> getCashTotals() {
		return cashTotals;
	}

	public void setCashTotals(List<Double> cashTotals) {
		this.cashTotals = cashTotals;
	}

	public List<Double> getCardTotals() {
		return cardTotals;
	}

	public void setCardTotals(List<Double> cardTotals) {
		this.cardTotals = cardTotals;
	}

	public void paintComponent(Graphics g) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Calendar c = Calendar.getInstance();

		java.util.Date lastDate = null, earlyDate = null;
		try {
			earlyDate = sdf.parse(dt1);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			c.setTime(sdf.parse(dt1));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			lastDate = sdf.parse(dt2);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int MILLSECS_PER_DAY = 24 * 60 * 60 * 1000;
		int deltaDays = (int) ((lastDate.getTime() - earlyDate.getTime()) / MILLSECS_PER_DAY);
		int bosluk = 0;
		int artis = 0;
		double maxvalue = 0.0;
		double oran = 0.0;
		int thickness = 4;
		int scale = 0;

		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setPaint(Color.black);
		g2d.draw(new Line2D.Double(12, 50, 12, 250));
		g2d.drawString("Amount", 20, 50);
		g2d.draw(new Line2D.Double(12, 250, 500, 250));
		g2d.drawString("Date", 495, 260);

		if (deltaDays <= 7) {
			scale = deltaDays;
			artis = 1;
		} else if (deltaDays > 7 && deltaDays < 30) {
			if ((deltaDays % 7) == 0) {
				scale = (deltaDays / 7);
			} else {
				scale = (deltaDays / 7) + 1;
			}
			artis = 7;
		} else if (deltaDays >= 30) {
			if ((deltaDays % 30) == 0) {
				scale = (deltaDays / 30);
			} else {
				scale = (deltaDays / 30) + 1;
			}
			artis = 30;
		}

		for (int j = 0; j < scale; j++) {
			if (cashTotals.get(j) > maxvalue)
				maxvalue = cashTotals.get(j);
		}
		for (int j = 0; j < scale; j++) {
			if (cardTotals.get(j) > maxvalue)
				maxvalue = cardTotals.get(j);
		}

		if (maxvalue > 100) {
			oran = ((maxvalue - 100) / maxvalue);
		} else
			oran = 0.0;

		for (int k = 0; k < scale; k++) {

			g.setColor(Color.yellow);
			g.fill3DRect((30 + bosluk), (245 - cashTotals.get(k).intValue())
					+ (int) (cashTotals.get(k) * oran), 25, cashTotals.get(k)
					.intValue()
					- (int) (cashTotals.get(k) * oran), false);
			for (int i = 1; i <= thickness; i++)
				g.draw3DRect((30 + bosluk) - i, 245
						- cashTotals.get(k).intValue()
						+ (int) (cashTotals.get(k) * oran) - i, 25 + 2 * i - 1,
						cashTotals.get(k).intValue()
								- (int) (cashTotals.get(k) * oran) + 2 * i - 1,
						true);
			g.setColor(Color.black);
			g.drawString("cash", (30 + bosluk), 245
					- cashTotals.get(k).intValue()
					+ (int) (cashTotals.get(k) * oran) - 3);
			g.drawString(String.valueOf(cashTotals.get(k)), (30 + bosluk), 245
					- cashTotals.get(k).intValue()
					+ (int) (cashTotals.get(k) * oran) + 10);
			g.drawString(
					String.valueOf(new java.sql.Date(earlyDate.getTime())),
					(4 + bosluk), 260);
			c.setTime(earlyDate);
			c.add(Calendar.DATE, 1);
			dt1 = sdf.format(c.getTime());
			try {
				earlyDate = sdf.parse(dt1);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (artis == 7 || artis == 30) {
				c.setTime(earlyDate);
				c.add(Calendar.DATE, (artis - 1));
				dt1 = sdf.format(c.getTime());
				try {
					earlyDate = sdf.parse(dt1);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g.drawString(
						"/"
								+ String.valueOf(new java.sql.Date(earlyDate
										.getTime())), (65 + bosluk), 260);
			}

			g.setColor(Color.red);
			g.fill3DRect((65 + bosluk), 245 - cardTotals.get(k).intValue()
					+ (int) (cardTotals.get(k) * oran), 25, cardTotals.get(k)
					.intValue()
					- (int) (cardTotals.get(k) * oran), false);
			for (int i = 1; i <= thickness; i++)
				g.draw3DRect((65 + bosluk) - i, 245
						- cardTotals.get(k).intValue()
						+ (int) (cardTotals.get(k) * oran) - i, 25 + 2 * i - 1,
						cardTotals.get(k).intValue()
								- (int) (cardTotals.get(k) * oran) + 2 * i - 1,
						true);
			g.setColor(Color.black);
			g.drawString("card", (65 + bosluk), 245
					- cardTotals.get(k).intValue()
					+ (int) (cardTotals.get(k) * oran) - 3);
			g.drawString(String.valueOf(cardTotals.get(k)), (65 + bosluk), 245
					- cardTotals.get(k).intValue()
					+ (int) (cardTotals.get(k) * oran) + 10);

			bosluk = bosluk + 140;
		}
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setTitle("Sales Reports");
		frame.setSize(900, 300);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		Container contentPane = frame.getContentPane();
		contentPane.add(new BarChartView());
		frame.setVisible(true);
	}

}
